/**
 * LoginDAO class.
 */
package pits.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import pits.dto.UserDTO;
import pits.util.Constants;

/**
 * DAO class for PITSPortlet.
 * 
 * @author ltd
 * 
 */
public class LoginDAO {

	/**
	 * log property
	 */
	public static Logger log = Logger.getLogger("LoginDAO");

	/**
	 * Get the information of one specify user.
	 * 
	 * @param name
	 * @param pass
	 * @return UserDTO object
	 */
	public List<UserDTO> getUser(String name, String pass) {
		// log.info("getUser start");
		// new pits.util.Logger().write("getUser.start " + name + " " + pass);
		UserDTO dto = null;
		List<UserDTO> result = new ArrayList<UserDTO>();
		Connection conn = null;
		String query = Constants.BLANK_VALUE;
		ResultSet rs = null;
		PreparedStatement pStat = null;

		try {
			query = "select * " + "from user " + "where UserName=? and Password=? and isDeleted=0";
			conn = pits.util.Connection.getConnection();
			pStat = conn.prepareStatement(query);
			pStat.setString(1, name);
			pStat.setString(2, pass);
			rs = pStat.executeQuery();
			while (rs.next()) {
				dto = new UserDTO();
				dto.setEmpID(rs.getInt("EmpID"));
				dto.setUserName(rs.getString("UserName"));
				dto.setPassword(rs.getString("Password"));
				dto.setRole(rs.getString("Role"));
				result.add(dto);
			}
		} catch (Exception sqlEx) {
			log.error("LoginDAO.getUser " + sqlEx);
			// new pits.util.Logger().write(sqlEx.toString());
		} finally {
			try {
				rs.close();
				pStat.close();
				conn.close();
			} catch (SQLException sqlEx) {
				log.error("LoginDAO.getUser " + sqlEx);
				// new pits.util.Logger().write(sqlEx.toString());
			}
		}

		return result;
	}
}
